package com.springboot.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springboot.domain.Ebook;
import com.springboot.dto.req.EbookQueryRequestDTO;
import com.springboot.dto.req.EbookSaveRequest;
import com.springboot.dto.resp.EbookResponseDTO;
import com.springboot.dto.resp.PageResponse;
import com.springboot.dto.resp.Result;
import com.springboot.service.EbookService;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/ebook")
public class EbookController {
    @Resource
    private EbookService ebookService;

    //请求未分页方法
    @GetMapping
    public Result<List<EbookResponseDTO>> list() {
        List<Ebook> ebooks = ebookService.list();
        List<EbookResponseDTO> ebookResponseDTOS = BeanUtil.copyToList(ebooks, EbookResponseDTO.class);
        return Result.success(ebookResponseDTOS);
    }
    // 查询全部电子书
// 带分页和条件查询
@RequestMapping("selectByPage")
public Result<PageResponse<EbookResponseDTO>> list(@Valid  EbookQueryRequestDTO ebookQueryRequestDTO) {
    // 查询条件
    QueryWrapper<Ebook> queryWrapper = new QueryWrapper<>();
    queryWrapper.like(ebookQueryRequestDTO.getName() != null,"name",ebookQueryRequestDTO.getName());
    queryWrapper.eq(ebookQueryRequestDTO.getCategory1Id()!= null,"category1_id",ebookQueryRequestDTO.getCategory1Id());
    queryWrapper.eq(ebookQueryRequestDTO.getCategory2Id()!= null,"category2_id",ebookQueryRequestDTO.getCategory2Id());
    // 分页信息
    Page<Ebook> page = new Page<>(ebookQueryRequestDTO.getPage(),ebookQueryRequestDTO.getSize());
    // 分页查询
    Page<Ebook> ebookPage = ebookService.page(page,queryWrapper);
    // 获取结果集
    List<Ebook> ebooks = ebookPage.getRecords();
    //日志输出总记录数和总页数
//    log.info("总记录数:{},总页数:{}",ebookPage.getTotal(),ebookPage.getPages());
    PageResponse<EbookResponseDTO> pageResponse = new PageResponse<>();
    // 转换成DTO
    List<EbookResponseDTO> ebookResponseDTOS = BeanUtil.copyToList(ebooks, EbookResponseDTO.class);
    // 设置分页信息
    pageResponse.setTotal(ebookPage.getTotal());
    // 设置结果集
    pageResponse.setList(ebookResponseDTOS);
    // 返回结果
    return Result.success(pageResponse);
}
    @PostMapping("/query")
    public Result<List<EbookResponseDTO>> query(@RequestBody EbookQueryRequestDTO ebookQueryRequestDTO) {
        QueryWrapper<Ebook> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(ebookQueryRequestDTO.getId() != null, "id", ebookQueryRequestDTO.getId());
//        queryWrapper.eq(ebookQueryRequestDTO.getName() !=  .like(ebookQueryRequestDTO.getName() != null, "name", ebookQueryRequestDTO.getName());
        queryWrapper.eq(ebookQueryRequestDTO.getCategory1Id() != null, "category1_id", ebookQueryRequestDTO.getCategory1Id());

        List<Ebook> ebooks = ebookService.list(queryWrapper);

        List<EbookResponseDTO> ebookResponseDTOS = ebooks.stream().map(ebook -> {
            EbookResponseDTO ebookResponseDTO = new EbookResponseDTO();
            BeanUtils.copyProperties(ebook, ebookResponseDTO);
            return ebookResponseDTO;
        }).toList();
        return Result.success(ebookResponseDTOS);
    }
//    //新增,校验注解
//    @PostMapping
//    public Result save(@Valid @RequestBody Ebook ebook) {
//        ebookService.save(ebook);
//        return Result.success();
//    }
    //新增或更新添加名称非空校验
    @PostMapping("saveOrUpdate")
    public Result<String> save(@Valid @RequestBody EbookSaveRequest ebookSaveRequest) {
        Ebook ebook = BeanUtil.copyProperties(ebookSaveRequest, Ebook.class);
        if (ebook == null) {
            return Result.failure(400, "参数为空");
        }
        // 保存或更新
        ebookService.saveOrUpdate(ebook);
        return Result.success();
    }
    //根据id删除
    @PostMapping("deleteById/{id}")
    public Result deleteById(@PathVariable Long id) {
        ebookService.removeById(id);
        return Result.success();
    }


}
